/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.google.common.base.Objects;
/*     */ import com.google.common.base.Objects.ToStringHelper;
/*     */ import com.google.common.collect.Sets;
/*     */ import com.zimbra.common.mailbox.Color;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.service.ServiceException.Argument;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.db.DbMailItem;
/*     */ import com.zimbra.cs.db.DbTag;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import com.zimbra.cs.mailbox.util.TypedIdList;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.session.Session.Type;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collections;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class Conversation
/*     */   extends MailItem
/*     */ {
/*     */   private String mEncodedSenders;
/*     */   protected SenderList mSenderList;
/*     */   private static final int RECALCULATE_CHANGE_MASK = 526359;
/*     */   
/*     */   Conversation(Mailbox mbox, MailItem.UnderlyingData data)
/*     */     throws ServiceException
/*     */   {
/*  49 */     this(mbox, data, false);
/*     */   }
/*     */   
/*     */   Conversation(Mailbox mbox, MailItem.UnderlyingData data, boolean skipCache) throws ServiceException {
/*  53 */     super(mbox, data, skipCache);
/*  54 */     if ((this.mData.type != MailItem.Type.CONVERSATION.toByte()) && (this.mData.type != MailItem.Type.VIRTUAL_CONVERSATION.toByte())) {
/*  55 */       throw new IllegalArgumentException();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   String getNormalizedSubject()
/*     */   {
/*  66 */     return ParsedMessage.normalize(getSubject());
/*     */   }
/*     */   
/*     */   public String getSender()
/*     */   {
/*  71 */     return "";
/*     */   }
/*     */   
/*     */   public String getSortSubject()
/*     */   {
/*  76 */     return getNormalizedSubject();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public int getMessageCount()
/*     */   {
/*  84 */     return (int)this.mData.size;
/*     */   }
/*     */   
/*     */   public int getInternalFlagBitmask()
/*     */   {
/*  89 */     return 0;
/*     */   }
/*     */   
/*     */   SenderList getSenderList()
/*     */     throws ServiceException
/*     */   {
/*  95 */     loadSenderList();
/*  96 */     return this.mSenderList;
/*     */   }
/*     */   
/*     */   void instantiateSenderList() {
/* 100 */     if ((this.mSenderList != null) && (this.mSenderList.size() == this.mData.size)) {
/* 101 */       return;
/*     */     }
/* 103 */     this.mSenderList = null;
/*     */     
/* 105 */     if (this.mEncodedSenders != null) {
/* 106 */       String encoded = this.mEncodedSenders;
/* 107 */       this.mEncodedSenders = null;
/*     */       
/*     */       try
/*     */       {
/* 111 */         this.mSenderList = SenderList.parse(encoded);
/* 112 */         if (this.mSenderList.size() != this.mData.size) {
/* 113 */           this.mSenderList = null;
/*     */         }
/*     */       }
/*     */       catch (Exception e) {}
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   boolean loadSenderList()
/*     */     throws ServiceException
/*     */   {
/* 128 */     instantiateSenderList();
/* 129 */     if (this.mSenderList != null) {
/* 130 */       return false;
/*     */     }
/*     */     
/*     */ 
/* 134 */     recalculateMetadata();
/* 135 */     return true;
/*     */   }
/*     */   
/*     */   void recalculateCounts(List<Message> msgs) throws ServiceException {
/* 139 */     markItemModified(7);
/* 140 */     Set<String> tags = new HashSet();
/* 141 */     this.mData.unreadCount = 0;
/* 142 */     this.mData.setFlags(0);
/* 143 */     for (Message msg : msgs) {
/* 144 */       this.mData.unreadCount += msg.getUnreadCount();
/* 145 */       this.mData.setFlags(this.mData.getFlags() | msg.getInternalFlagBitmask());
/* 146 */       for (String tag : msg.mData.getTags()) {
/* 147 */         tags.add(tag);
/*     */       }
/*     */     }
/* 150 */     this.mData.setTags(new Tag.NormalizedTags(tags));
/*     */   }
/*     */   
/*     */ 
/*     */   SenderList recalculateMetadata()
/*     */     throws ServiceException
/*     */   {
/* 157 */     return recalculateMetadata(getMessages());
/*     */   }
/*     */   
/*     */   SenderList recalculateMetadata(List<Message> msgs) throws ServiceException {
/* 161 */     Collections.sort(msgs, new MailItem.SortDateAscending());
/*     */     
/* 163 */     markItemModified(526359);
/*     */     
/* 165 */     this.mEncodedSenders = null;
/* 166 */     this.mSenderList = new SenderList(msgs);
/* 167 */     this.mData.size = msgs.size();
/*     */     
/* 169 */     Set<String> tags = new HashSet();
/* 170 */     this.mData.unreadCount = 0;
/* 171 */     this.mData.setFlags(0);
/* 172 */     this.mExtendedData = null;
/* 173 */     for (Message msg : msgs) {
/* 174 */       super.addChild(msg);
/* 175 */       this.mData.unreadCount += (msg.isUnread() ? 1 : 0);
/* 176 */       this.mData.setFlags(this.mData.getFlags() | msg.getInternalFlagBitmask());
/* 177 */       for (String tag : msg.mData.getTags()) {
/* 178 */         tags.add(tag);
/*     */       }
/* 180 */       this.mExtendedData = MetadataCallback.duringConversationAdd(this.mExtendedData, msg);
/*     */     }
/* 182 */     this.mData.setTags(new Tag.NormalizedTags(tags));
/*     */     
/*     */ 
/* 185 */     ZimbraLog.mailbox.debug("resetting metadata: cid=%d,size was=%d is=%d", new Object[] { Integer.valueOf(this.mId), Long.valueOf(this.mData.size), Integer.valueOf(this.mSenderList.size()) });
/* 186 */     saveData(new DbMailItem(this.mMailbox));
/* 187 */     return this.mSenderList;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   List<Message> getMessages()
/*     */     throws ServiceException
/*     */   {
/* 195 */     return getMessages(SortBy.NONE, -1);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public List<Message> getMessages(SortBy sort, int limit)
/*     */     throws ServiceException
/*     */   {
/* 206 */     List<Message> msgs = new ArrayList(getMessageCount());
/* 207 */     List<MailItem.UnderlyingData> listData = DbMailItem.getByParent(this, sort, limit, false);
/* 208 */     for (MailItem.UnderlyingData data : listData) {
/* 209 */       msgs.add(this.mMailbox.getMessage(data));
/*     */     }
/* 211 */     return msgs;
/*     */   }
/*     */   
/*     */   boolean canAccess(short rightsNeeded)
/*     */   {
/* 216 */     return true;
/*     */   }
/*     */   
/*     */   boolean canAccess(short rightsNeeded, Account authuser, boolean asAdmin)
/*     */   {
/* 221 */     return true;
/*     */   }
/*     */   
/*     */   boolean isTaggable()
/*     */   {
/* 226 */     return false;
/*     */   }
/*     */   
/*     */   boolean isCopyable()
/*     */   {
/* 231 */     return false;
/*     */   }
/*     */   
/*     */   boolean isMovable()
/*     */   {
/* 236 */     return true;
/*     */   }
/*     */   
/*     */   boolean isMutable()
/*     */   {
/* 241 */     return true;
/*     */   }
/*     */   
/*     */   boolean canHaveChildren()
/*     */   {
/* 246 */     return true;
/*     */   }
/*     */   
/*     */   boolean canParent(MailItem item)
/*     */   {
/* 251 */     return item instanceof Message;
/*     */   }
/*     */   
/*     */   static Conversation create(Mailbox mbox, int id, Message... msgs) throws ServiceException {
/* 255 */     if (ZimbraLog.mailop.isDebugEnabled()) {
/* 256 */       StringBuilder msgIds = new StringBuilder();
/* 257 */       for (int i = 0; i < msgs.length; i++) {
/* 258 */         msgIds.append(i > 0 ? "," : "").append(msgs[i].getId());
/*     */       }
/* 260 */       ZimbraLog.mailop.debug("Adding Conversation: id=%d, message(s): %s.", new Object[] { Integer.valueOf(id), msgIds });
/*     */     }
/*     */     
/* 263 */     assert ((id != -1) && (msgs.length > 0));
/* 264 */     Arrays.sort(msgs, new MailItem.SortDateAscending());
/*     */     
/* 266 */     int date = 0;int unread = 0;int flags = 0;
/* 267 */     MailItem.CustomMetadata.CustomMetadataList extended = null;
/* 268 */     Set<String> tags = new HashSet();
/* 269 */     for (int i = 0; i < msgs.length; i++) {
/* 270 */       Message msg = msgs[i];
/* 271 */       if (msg == null) {
/* 272 */         throw ServiceException.FAILURE("null Message in list", null);
/*     */       }
/* 274 */       date = Math.max(date, msg.mData.date);
/* 275 */       unread += msg.mData.unreadCount;
/* 276 */       flags |= msg.mData.getFlags();
/* 277 */       for (String tag : msg.mData.getTags()) {
/* 278 */         tags.add(tag);
/*     */       }
/* 280 */       extended = MetadataCallback.duringConversationAdd(extended, msg);
/*     */     }
/*     */     
/* 283 */     MailItem.UnderlyingData data = new MailItem.UnderlyingData();
/* 284 */     data.id = id;
/* 285 */     data.type = MailItem.Type.CONVERSATION.toByte();
/* 286 */     data.folderId = 9;
/* 287 */     data.setSubject(msgs.length > 0 ? msgs[0].getSubject() : "");
/* 288 */     data.date = date;
/* 289 */     data.size = msgs.length;
/* 290 */     data.unreadCount = unread;
/* 291 */     data.setFlags(flags);
/* 292 */     data.setTags(new Tag.NormalizedTags(tags));
/* 293 */     data.metadata = encodeMetadata(DEFAULT_COLOR_RGB, 1, 1, extended, new SenderList(msgs));
/* 294 */     data.contentChanged(mbox);
/* 295 */     new DbMailItem(mbox).create(data);
/*     */     
/* 297 */     Conversation conv = new Conversation(mbox, data);
/* 298 */     conv.finishCreation(null);
/*     */     
/* 300 */     DbMailItem.setParent(msgs, conv);
/* 301 */     for (int i = 0; i < msgs.length; i++) {
/* 302 */       mbox.markItemModified(msgs[i], 512);
/* 303 */       msgs[i].mData.parentId = id;
/* 304 */       msgs[i].metadataChanged();
/*     */     }
/* 306 */     return conv;
/*     */   }
/*     */   
/*     */   void open(String hash) throws ServiceException {
/* 310 */     DbMailItem.openConversation(hash, this);
/*     */   }
/*     */   
/*     */   void close(String hash) throws ServiceException {
/* 314 */     DbMailItem.closeConversation(hash, this);
/*     */   }
/*     */   
/*     */   void detach() throws ServiceException
/*     */   {
/* 319 */     close(Mailbox.getHash(getNormalizedSubject()));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   void alterUnread(boolean unread)
/*     */     throws ServiceException
/*     */   {
/* 342 */     markItemModified(1);
/*     */     
/* 344 */     boolean excludeAccess = false;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 349 */     MailItem.TargetConstraint tcon = this.mMailbox.getOperationTargetConstraint();
/* 350 */     List<Integer> targets = new ArrayList();
/* 351 */     for (Message msg : getMessages())
/*     */     {
/* 353 */       if (msg.isUnread() != unread)
/*     */       {
/* 355 */         if (!msg.canAccess((short)2)) {
/* 356 */           excludeAccess = true;
/* 357 */         } else if ((msg.checkChangeID()) && (MailItem.TargetConstraint.checkItem(tcon, msg)))
/*     */         {
/*     */ 
/*     */ 
/* 361 */           int delta = unread ? 1 : -1;
/* 362 */           msg.updateUnread(delta, msg.isTagged(Flag.FlagInfo.DELETED) ? delta : 0);
/* 363 */           msg.metadataChanged();
/* 364 */           targets.add(Integer.valueOf(msg.getId()));
/*     */         }
/*     */       }
/*     */     }
/* 368 */     if (targets.isEmpty()) {
/* 369 */       if (excludeAccess) {
/* 370 */         throw ServiceException.PERM_DENIED("you do not have sufficient permissions");
/*     */       }
/*     */     } else {
/* 373 */       DbMailItem.alterUnread(this.mMailbox, targets, unread);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   void alterTag(Tag tag, boolean add)
/*     */     throws ServiceException
/*     */   {
/* 397 */     if (tag == null) {
/* 398 */       throw ServiceException.FAILURE("missing tag argument", null);
/*     */     }
/* 400 */     if ((!add) && (!isTagged(tag))) {
/* 401 */       return;
/*     */     }
/* 403 */     if (tag.getId() == Flag.ID_UNREAD) {
/* 404 */       throw ServiceException.FAILURE("unread state must be set with alterUnread", null);
/*     */     }
/*     */     
/* 407 */     if (((tag instanceof Flag)) && (((Flag)tag).isSystemFlag())) {
/* 408 */       throw MailServiceException.CANNOT_TAG(tag, this);
/*     */     }
/* 410 */     markItemModified((tag instanceof Flag) ? 4 : 2);
/*     */     
/* 412 */     MailItem.TargetConstraint tcon = this.mMailbox.getOperationTargetConstraint();
/* 413 */     boolean excludeAccess = false;
/*     */     
/* 415 */     List<Message> msgs = getMessages();
/* 416 */     List<Integer> targets = new ArrayList(msgs.size());
/* 417 */     for (Message msg : msgs)
/*     */     {
/* 419 */       if (msg.isTagged(tag) != add)
/*     */       {
/* 421 */         if (!msg.canAccess((short)2)) {
/* 422 */           excludeAccess = true;
/* 423 */         } else if ((msg.checkChangeID()) && (MailItem.TargetConstraint.checkItem(tcon, msg)))
/*     */         {
/* 425 */           if ((add) && (!tag.canTag(msg))) {
/* 426 */             throw MailServiceException.CANNOT_TAG(tag, this);
/*     */           }
/*     */           
/* 429 */           targets.add(Integer.valueOf(msg.getId()));
/* 430 */           msg.tagChanged(tag, add);
/*     */           
/*     */ 
/* 433 */           int delta = add ? 1 : -1;
/* 434 */           if ((tag.trackUnread()) && (msg.isUnread())) {
/* 435 */             tag.updateUnread(delta, isTagged(Flag.FlagInfo.DELETED) ? delta : 0);
/*     */           }
/*     */           
/*     */ 
/* 439 */           if (tag.getId() == Flag.ID_DELETED) {
/* 440 */             getFolder().updateSize(0, delta, 0L);
/*     */             
/* 442 */             if (msg.isUnread())
/* 443 */               msg.updateUnread(0, delta);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 448 */     if (targets.isEmpty()) {
/* 449 */       if (excludeAccess) {
/* 450 */         throw ServiceException.PERM_DENIED("you do not have sufficient permissions");
/*     */       }
/*     */     } else {
/* 453 */       if (ZimbraLog.mailop.isDebugEnabled()) {
/* 454 */         String operation = add ? "Setting" : "Unsetting";
/* 455 */         ZimbraLog.mailop.debug("%s %s for %s.  Affected ids: %s", new Object[] { operation, getMailopContext(tag), getMailopContext(this), StringUtil.join(",", targets) });
/*     */       }
/*     */       
/* 458 */       recalculateCounts(msgs);
/* 459 */       DbTag.alterTag(tag, targets, add);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void inheritedTagChanged(Tag tag, boolean add) throws ServiceException
/*     */   {
/* 465 */     if ((tag == null) || (add == isTagged(tag))) {
/* 466 */       return;
/*     */     }
/* 468 */     markItemModified((tag instanceof Flag) ? 4 : 2);
/* 469 */     if (add) {
/* 470 */       tagChanged(tag, add);
/*     */     } else {
/* 472 */       DbMailItem.completeConversation(this.mMailbox, this.mMailbox.getOperationConnection(), this.mData);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void inheritedCustomDataChanged(Message msg, MailItem.CustomMetadata custom) throws ServiceException {
/* 477 */     if (custom == null) {
/* 478 */       return;
/*     */     }
/* 480 */     markItemModified(524288);
/* 481 */     if (!custom.isEmpty()) {
/* 482 */       this.mExtendedData = MetadataCallback.duringConversationAdd(this.mExtendedData, msg);
/* 483 */       saveMetadata();
/*     */     } else {
/* 485 */       recalculateMetadata();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   boolean move(Folder target)
/*     */     throws ServiceException
/*     */   {
/* 513 */     if (!target.canContain(MailItem.Type.MESSAGE)) {
/* 514 */       throw MailServiceException.CANNOT_CONTAIN();
/*     */     }
/* 516 */     markItemModified(0);
/*     */     
/* 518 */     List<Message> msgs = getMessages();
/* 519 */     MailItem.TargetConstraint tcon = this.mMailbox.getOperationTargetConstraint();
/* 520 */     boolean toTrash = target.inTrash();
/* 521 */     int oldUnread = 0;
/* 522 */     for (Message msg : msgs) {
/* 523 */       if (msg.isUnread()) {
/* 524 */         oldUnread++;
/*     */       }
/*     */     }
/*     */     
/* 528 */     this.mData.unreadCount = oldUnread;
/*     */     
/* 530 */     boolean excludeAccess = false;
/*     */     
/* 532 */     List<Integer> markedRead = new ArrayList();
/* 533 */     List<Message> moved = new ArrayList();
/* 534 */     List<MailItem> indexUpdated = new ArrayList();
/*     */     
/* 536 */     for (Message msg : msgs) {
/* 537 */       Folder source = msg.getFolder();
/*     */       
/*     */ 
/* 540 */       if (source.getId() != target.getId())
/*     */       {
/* 542 */         if (!source.canAccess((short)8)) {
/* 543 */           excludeAccess = true;
/* 544 */         } else if ((target.getId() != 3) && (target.getId() != 4) && (!target.canAccess((short)4))) {
/* 545 */           excludeAccess = true;
/* 546 */         } else if ((msg.checkChangeID()) && (MailItem.TargetConstraint.checkItem(tcon, msg)))
/*     */         {
/*     */ 
/*     */ 
/* 550 */           boolean isDeleted = msg.isTagged(Flag.FlagInfo.DELETED);
/* 551 */           if (msg.isUnread()) {
/* 552 */             if ((!toTrash) || (msg.inTrash())) {
/* 553 */               source.updateUnread(-1, isDeleted ? -1 : 0);
/* 554 */               target.updateUnread(1, isDeleted ? 1 : 0);
/*     */             }
/*     */             else
/*     */             {
/* 558 */               msg.updateUnread(-1, isDeleted ? -1 : 0);
/*     */               
/* 560 */               markedRead.add(Integer.valueOf(msg.getId()));
/*     */             }
/*     */           }
/*     */           
/*     */ 
/* 565 */           if ((msg.inSpam()) && (!target.inSpam()) && (msg.getIndexStatus() != MailItem.IndexStatus.NO)) {
/* 566 */             indexUpdated.add(msg);
/*     */           }
/*     */           
/*     */ 
/* 570 */           if ((toTrash) && (msg.isDraft())) {
/* 571 */             msg.setDraftAutoSendTime(0L);
/*     */           }
/*     */           
/*     */ 
/* 575 */           source.updateSize(-1, isDeleted ? -1 : 0, -msg.getTotalSize());
/* 576 */           target.updateSize(1, isDeleted ? 1 : 0, msg.getTotalSize());
/*     */           
/* 578 */           moved.add(msg);
/*     */         }
/*     */       }
/*     */     }
/* 582 */     if (!markedRead.isEmpty()) {
/* 583 */       DbMailItem.alterUnread(target.getMailbox(), markedRead, false);
/*     */     }
/*     */     
/* 586 */     if (moved.isEmpty()) {
/* 587 */       if (excludeAccess) {
/* 588 */         throw ServiceException.PERM_DENIED("you do not have sufficient permissions");
/*     */       }
/*     */     } else {
/* 591 */       if (target.inSpam()) {
/* 592 */         detach();
/*     */       }
/* 594 */       if (ZimbraLog.mailop.isInfoEnabled()) {
/* 595 */         StringBuilder ids = new StringBuilder();
/* 596 */         for (int i = 0; i < moved.size(); i++) {
/* 597 */           ids.append(i > 0 ? "," : "").append(((Message)moved.get(i)).getId());
/*     */         }
/* 599 */         ZimbraLog.mailop.info("Moving %s to %s.  Affected message ids: %s.", new Object[] { getMailopContext(this), getMailopContext(target), ids });
/*     */       }
/*     */       
/* 602 */       DbMailItem.setFolder(moved, target);
/* 603 */       for (Message msg : moved) {
/* 604 */         msg.folderChanged(target, 0);
/*     */       }
/*     */       
/* 607 */       if (!indexUpdated.isEmpty()) {
/* 608 */         for (MailItem msg : indexUpdated) {
/* 609 */           this.mMailbox.index.add(msg);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 614 */     return !moved.isEmpty();
/*     */   }
/*     */   
/*     */   void addChild(MailItem child)
/*     */     throws ServiceException
/*     */   {
/* 620 */     if (!(child instanceof Message)) {
/* 621 */       throw MailServiceException.CANNOT_PARENT();
/*     */     }
/* 623 */     Message msg = (Message)child;
/*     */     
/* 625 */     super.addChild(msg);
/*     */     
/*     */ 
/* 628 */     int oldFlags = this.mData.getFlags();
/* 629 */     this.mData.setFlags(this.mData.getFlags() | msg.getInternalFlagBitmask());
/* 630 */     if (this.mData.getFlags() != oldFlags) {
/* 631 */       markItemModified(4);
/*     */     }
/*     */     
/*     */ 
/* 635 */     String[] msgTags = msg.mData.getTags();
/* 636 */     if (msgTags.length > 0) {
/* 637 */       Set<String> tags = Sets.newHashSet(this.mData.getTags());
/* 638 */       int oldCount = tags.size();
/* 639 */       for (String msgTag : msgTags) {
/* 640 */         tags.add(msgTag);
/*     */       }
/* 642 */       if (tags.size() != oldCount) {
/* 643 */         markItemModified(2);
/* 644 */         this.mData.setTags(new Tag.NormalizedTags(tags));
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 649 */     if (msg.isUnread()) {
/* 650 */       markItemModified(1);
/* 651 */       updateUnread(child.mData.unreadCount, child.isTagged(Flag.FlagInfo.DELETED) ? child.mData.unreadCount : 0);
/*     */     }
/*     */     
/* 654 */     markItemModified(526352);
/*     */     
/* 656 */     MetadataCallback.duringConversationAdd(this.mExtendedData, msg);
/*     */     
/*     */ 
/*     */ 
/* 660 */     this.mData.date = this.mMailbox.getOperationTimestamp();
/* 661 */     contentChanged();
/*     */     
/* 663 */     if (!this.mMailbox.hasListeners(Session.Type.SOAP)) {
/* 664 */       instantiateSenderList();
/* 665 */       this.mData.size += 1L;
/*     */       try {
/* 667 */         if (this.mSenderList != null) {
/* 668 */           this.mSenderList.add(msg);
/*     */         }
/*     */       } catch (SenderList.RefreshException slre) {
/* 671 */         this.mSenderList = null;
/*     */       }
/* 673 */       saveMetadata();
/*     */     } else {
/* 675 */       boolean recalculated = loadSenderList();
/* 676 */       if (!recalculated) {
/* 677 */         this.mData.size += 1L;
/*     */         try {
/* 679 */           this.mSenderList.add(msg);
/* 680 */           saveMetadata();
/*     */         } catch (SenderList.RefreshException slre) {
/* 682 */           recalculateMetadata();
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   void removeChild(MailItem child) throws ServiceException
/*     */   {
/* 690 */     super.removeChild(child);
/*     */     
/*     */ 
/* 693 */     if (getMessageCount() == 0) {
/* 694 */       delete();
/* 695 */       return;
/*     */     }
/*     */     
/* 698 */     markItemModified(2064);
/*     */     
/* 700 */     if (!this.mMailbox.hasListeners(Session.Type.SOAP))
/*     */     {
/* 702 */       if (child.isUnread()) {
/* 703 */         markItemModified(1);
/* 704 */         updateUnread(-child.mData.unreadCount, child.isTagged(Flag.FlagInfo.DELETED) ? -child.mData.unreadCount : 0);
/*     */       }
/*     */       
/*     */ 
/* 708 */       if ((child.mData.getTags().length != 0) || (child.mData.getFlags() != 0)) {
/* 709 */         int oldFlags = this.mData.getFlags();
/* 710 */         int oldTagCount = this.mData.getTags().length;
/*     */         
/* 712 */         DbMailItem.completeConversation(this.mMailbox, this.mMailbox.getOperationConnection(), this.mData);
/*     */         
/* 714 */         if (this.mData.getFlags() != oldFlags) {
/* 715 */           markItemModified(4);
/*     */         }
/* 717 */         if (this.mData.getTags().length != oldTagCount) {
/* 718 */           markItemModified(2);
/*     */         }
/*     */       }
/*     */       
/* 722 */       this.mEncodedSenders = null;
/* 723 */       this.mSenderList = null;
/* 724 */       this.mData.size -= 1L;
/* 725 */       saveMetadata(null);
/*     */     } else {
/* 727 */       List<Message> msgs = getMessages();
/* 728 */       msgs.remove(child);
/* 729 */       recalculateMetadata(msgs);
/*     */     }
/*     */   }
/*     */   
/*     */   void merge(Conversation other) throws ServiceException {
/* 734 */     if (other == this) {
/* 735 */       return;
/*     */     }
/*     */     
/*     */ 
/* 739 */     markItemModified(268435456);
/*     */     
/* 741 */     for (Message msg : other.getMessages()) {
/* 742 */       msg.markItemModified(512);
/* 743 */       msg.mData.parentId = this.mId;
/* 744 */       MetadataCallback.duringConversationAdd(this.mExtendedData, msg);
/*     */     }
/* 746 */     DbMailItem.reparentChildren(other, this);
/* 747 */     DbMailItem.changeOpenTargets(other, getId());
/*     */     
/* 749 */     recalculateMetadata();
/*     */     
/*     */ 
/* 752 */     if ((other instanceof VirtualConversation)) {
/* 753 */       other.removeChild(((VirtualConversation)other).getMessage());
/*     */     } else {
/* 755 */       other.delete();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   MailItem.PendingDelete getDeletionInfo()
/*     */     throws ServiceException
/*     */   {
/* 782 */     MailItem.PendingDelete info = new MailItem.PendingDelete();
/* 783 */     info.itemIds.add(getType(), Integer.valueOf(this.mId), this.mData.uuid);
/*     */     
/* 785 */     if (this.mData.size == 0L) {
/* 786 */       return info;
/*     */     }
/*     */     
/* 789 */     List<Message> msgs = getMessages();
/* 790 */     MailItem.TargetConstraint tcon = this.mMailbox.getOperationTargetConstraint();
/*     */     
/* 792 */     boolean excludeModify = false;boolean excludeAccess = false;
/* 793 */     for (Message child : msgs)
/*     */     {
/* 795 */       if (MailItem.TargetConstraint.checkItem(tcon, child))
/*     */       {
/* 797 */         if (!child.canAccess((short)8)) {
/* 798 */           excludeAccess = true;
/* 799 */         } else if (!child.checkChangeID()) {
/* 800 */           excludeModify = true;
/*     */         } else {
/* 802 */           info.add(child.getDeletionInfo());
/*     */         }
/*     */       }
/*     */     }
/* 806 */     int totalDeleted = info.itemIds.size();
/* 807 */     if (totalDeleted == 1)
/*     */     {
/* 809 */       if (excludeAccess)
/* 810 */         throw ServiceException.PERM_DENIED("you do not have sufficient permissions");
/* 811 */       if (excludeModify) {
/* 812 */         throw MailServiceException.MODIFY_CONFLICT(new ServiceException.Argument[0]);
/*     */       }
/*     */     }
/* 815 */     if (totalDeleted != msgs.size() + 1) {
/* 816 */       info.incomplete = true;
/*     */     }
/* 818 */     return info;
/*     */   }
/*     */   
/*     */   void purgeCache(MailItem.PendingDelete info, boolean purgeItem)
/*     */     throws ServiceException
/*     */   {
/* 824 */     if (info.incomplete) {
/* 825 */       recalculateMetadata();
/*     */     }
/*     */     
/* 828 */     super.purgeCache(info, purgeItem);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   void decodeMetadata(String metadata)
/*     */   {
/* 836 */     if (metadata != null) {
/*     */       try {
/* 838 */         Metadata meta = new Metadata(metadata, Integer.valueOf(this.mId));
/* 839 */         if (meta.containsKey("prt")) {
/* 840 */           decodeMetadata(meta);
/*     */         }
/*     */       } catch (ServiceException e) {
/* 843 */         ZimbraLog.mailbox.error("Failed to parse metadata id=%d,type=%s", Integer.valueOf(this.mId), getType(), e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   void decodeMetadata(Metadata meta) throws ServiceException
/*     */   {
/* 850 */     super.decodeMetadata(meta);
/* 851 */     this.mEncodedSenders = meta.get("prt", null);
/*     */   }
/*     */   
/*     */   Metadata encodeMetadata(Metadata meta)
/*     */   {
/* 856 */     String encoded = this.mEncodedSenders;
/* 857 */     if ((encoded == null) && (this.mSenderList != null)) {
/* 858 */       encoded = this.mSenderList.toString();
/*     */     }
/* 860 */     return encodeMetadata(meta, this.mRGBColor, this.mMetaVersion, this.mVersion, this.mExtendedData, encoded);
/*     */   }
/*     */   
/*     */   static String encodeMetadata(Color color, int metaVersion, int version, MailItem.CustomMetadata.CustomMetadataList extended, SenderList senders) {
/* 864 */     return encodeMetadata(new Metadata(), color, metaVersion, version, extended, senders.toString()).toString();
/*     */   }
/*     */   
/*     */   static Metadata encodeMetadata(Metadata meta, Color color, int metaVersion, int version, MailItem.CustomMetadata.CustomMetadataList extended, String encodedSenders) {
/* 868 */     meta.put("prt", encodedSenders);
/* 869 */     return MailItem.encodeMetadata(meta, color, null, metaVersion, version, extended);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected boolean isQuotaCheckRequired()
/*     */     throws ServiceException
/*     */   {
/* 879 */     Account account = getMailbox().getAccount();
/* 880 */     return !account.isMailAllowReceiveButNotSendWhenOverQuota();
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 885 */     return appendCommonMembers(Objects.toStringHelper(this)).toString();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/Conversation.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */